<template>
  <div class="material-flow-manage">
    <el-card shadow="hover">
      <div class="flow-header">
        <el-input
          v-model="searchText"
          placeholder="搜索物料名称/编码"
          clearable
          style="width:200px; margin-right:16px;"
          @keyup.enter="doSearch"
        />
        <el-select v-model="searchType" placeholder="操作类型" clearable style="width:130px; margin-right:16px;">
          <el-option v-for="item in typeOptions" :key="item" :label="item" :value="item"/>
        </el-select>
        <el-button type="primary" @click="handleAdd">新增流转</el-button>
      </div>
      <el-table :data="filteredList" border style="margin-top:16px;">
        <el-table-column prop="id" label="流转编号" width="120px"/>
        <el-table-column prop="materialName" label="物料名称" width="140px"/>
        <el-table-column prop="materialCode" label="物料编码" width="120px"/>
        <el-table-column prop="actionType" label="操作类型" width="100px"/>
        <el-table-column prop="quantity" label="操作数量" width="100px"/>
        <el-table-column prop="actionTime" label="操作时间" width="160px"/>
        <el-table-column prop="operator" label="操作人" width="100px"/>
        <el-table-column prop="remark" label="备注" width="140px"/>
        <el-table-column label="操作" width="160px" align="center">
          <template #default="scope">
            <el-button size="small" type="primary" text @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" text @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 新增/编辑弹窗 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="440px" destroy-on-close>
      <el-form :model="editRow" :rules="formRules" ref="formRef" label-width="90px">
        <el-form-item label="物料名称" prop="materialName">
          <el-input v-model="editRow.materialName"/>
        </el-form-item>
        <el-form-item label="物料编码" prop="materialCode">
          <el-input v-model="editRow.materialCode"/>
        </el-form-item>
        <el-form-item label="操作类型" prop="actionType">
          <el-select v-model="editRow.actionType" placeholder="请选择">
            <el-option v-for="item in typeOptions" :key="item" :label="item" :value="item"/>
          </el-select>
        </el-form-item>
        <el-form-item label="操作数量" prop="quantity">
          <el-input-number v-model="editRow.quantity" :min="1" style="width:100%;"/>
        </el-form-item>
        <el-form-item label="操作时间" prop="actionTime">
          <el-date-picker v-model="editRow.actionTime" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" style="width:100%;" />
        </el-form-item>
        <el-form-item label="操作人" prop="operator">
          <el-input v-model="editRow.operator"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="editRow.remark" type="textarea"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="submitEdit">保存</el-button>
      </template>
    </el-dialog>

    <!-- 删除弹窗 -->
    <el-dialog v-model="deleteVisible" title="提示" width="320px" center>
      <div>确定要删除该流转记录吗？</div>
      <template #footer>
        <el-button @click="deleteVisible=false">取消</el-button>
        <el-button type="danger" @click="confirmDelete">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, computed } from 'vue'
import { ElMessage } from 'element-plus'

interface FlowItem {
  id: string
  materialName: string
  materialCode: string
  actionType: string
  quantity: number
  actionTime: string
  operator: string
  remark: string
}

// 操作类型
const typeOptions = ['入库', '流转', '出库']

// 15条模拟数据
const flowList = ref<FlowItem[]>([
  { id: 'F001', materialName: 'HRB400钢筋', materialCode: 'M001', actionType: '入库', quantity: 50, actionTime: '2024-06-01 09:23:12', operator: '张三', remark: '采购入库' },
  { id: 'F002', materialName: 'PO42.5水泥', materialCode: 'M002', actionType: '入库', quantity: 80, actionTime: '2024-06-01 10:10:07', operator: '李四', remark: '' },
  { id: 'F003', materialName: 'HRB400钢筋', materialCode: 'M001', actionType: '流转', quantity: 20, actionTime: '2024-06-02 14:00:23', operator: '王五', remark: '用于1#楼' },
  { id: 'F004', materialName: '钢化玻璃', materialCode: 'M003', actionType: '入库', quantity: 30, actionTime: '2024-06-02 15:20:31', operator: '张三', remark: '玻璃采购' },
  { id: 'F005', materialName: 'HRB400钢筋', materialCode: 'M001', actionType: '出库', quantity: 15, actionTime: '2024-06-03 08:12:18', operator: '李四', remark: '1#楼施工用' },
  { id: 'F006', materialName: 'PO42.5水泥', materialCode: 'M002', actionType: '流转', quantity: 10, actionTime: '2024-06-03 10:42:50', operator: '王五', remark: '水泥调拨' },
  { id: 'F007', materialName: '石膏板', materialCode: 'M004', actionType: '入库', quantity: 40, actionTime: '2024-06-03 11:28:36', operator: '张三', remark: '' },
  { id: 'F008', materialName: '防水涂料', materialCode: 'M006', actionType: '入库', quantity: 20, actionTime: '2024-06-03 15:56:13', operator: '李四', remark: '' },
  { id: 'F009', materialName: '钢化玻璃', materialCode: 'M003', actionType: '流转', quantity: 12, actionTime: '2024-06-04 09:07:54', operator: '王五', remark: '' },
  { id: 'F010', materialName: 'HRB400钢筋', materialCode: 'M001', actionType: '流转', quantity: 8, actionTime: '2024-06-04 13:12:21', operator: '李四', remark: '' },
  { id: 'F011', materialName: 'BV铜线', materialCode: 'M005', actionType: '入库', quantity: 60, actionTime: '2024-06-05 08:30:00', operator: '王五', remark: '' },
  { id: 'F012', materialName: 'BV铜线', materialCode: 'M005', actionType: '流转', quantity: 20, actionTime: '2024-06-05 10:00:42', operator: '张三', remark: '分配至配电箱' },
  { id: 'F013', materialName: '石膏板', materialCode: 'M004', actionType: '出库', quantity: 15, actionTime: '2024-06-05 13:40:15', operator: '李四', remark: '' },
  { id: 'F014', materialName: 'PO42.5水泥', materialCode: 'M002', actionType: '出库', quantity: 8, actionTime: '2024-06-06 09:52:11', operator: '王五', remark: '' },
  { id: 'F015', materialName: '防水涂料', materialCode: 'M006', actionType: '流转', quantity: 10, actionTime: '2024-06-06 11:15:23', operator: '张三', remark: '' }
])

// 搜索
const searchText = ref('')
const searchType = ref('')
const filteredList = computed(() => {
  let arr = flowList.value
  if (searchText.value) {
    arr = arr.filter(
      r =>
        r.materialName.includes(searchText.value) ||
        r.materialCode.includes(searchText.value)
    )
  }
  if (searchType.value) {
    arr = arr.filter(r => r.actionType === searchType.value)
  }
  return arr
})
function doSearch() {}

// 新增/编辑弹窗
const dialogVisible = ref(false)
const dialogTitle = ref('新增流转')
const editRow = reactive<FlowItem>({
  id: '',
  materialName: '',
  materialCode: '',
  actionType: '',
  quantity: 1,
  actionTime: '',
  operator: '',
  remark: ''
})
const formRef = ref()
const formRules = {
  materialName: [{ required: true, message: '必填', trigger: 'blur' }],
  materialCode: [{ required: true, message: '必填', trigger: 'blur' }],
  actionType: [{ required: true, message: '必选', trigger: 'change' }],
  quantity: [{ required: true, message: '必填', trigger: 'blur' }],
  actionTime: [{ required: true, message: '必填', trigger: 'change' }],
  operator: [{ required: true, message: '必填', trigger: 'blur' }]
}

function handleAdd() {
  dialogTitle.value = '新增流转'
  Object.assign(editRow, { id: '', materialName: '', materialCode: '', actionType: '', quantity: 1, actionTime: '', operator: '', remark: '' })
  dialogVisible.value = true
}
function handleEdit(row: FlowItem) {
  dialogTitle.value = '编辑流转'
  Object.assign(editRow, row)
  dialogVisible.value = true
}
function submitEdit() {
  formRef.value?.validate((valid: boolean) => {
    if (!valid) return
    if (dialogTitle.value.startsWith('编辑')) {
      const idx = flowList.value.findIndex(t => t.id === editRow.id)
      if (idx !== -1) flowList.value[idx] = { ...editRow }
      ElMessage.success('编辑成功')
    } else {
      // 自动生成新编号
      editRow.id = 'F' + (flowList.value.length + 1).toString().padStart(3, '0')
      flowList.value.unshift({ ...editRow })
      ElMessage.success('新增成功')
    }
    dialogVisible.value = false
  })
}

// 删除
const deleteVisible = ref(false)
const deleteTarget = ref<FlowItem | null>(null)
function handleDelete(row: FlowItem) {
  deleteTarget.value = row
  deleteVisible.value = true
}
function confirmDelete() {
  if (deleteTarget.value) {
    flowList.value = flowList.value.filter(t => t.id !== deleteTarget.value!.id)
    ElMessage.success('删除成功')
  }
  deleteVisible.value = false
}
</script>

<style scoped>
.material-flow-manage {
  width: 100%;
  margin: 0;
  padding: 32px 0;
}
.flow-header {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
}
</style>
